// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
import evn from "../../utils/http/evn"
import { apiUpdateUserInfo } from '../../utils/http/api'
Page({
   /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl: evn.dev.imgUrl,
    userInfo: {
      avatarUrl:null,
      nickname:null,
      mobile:null
    },
    url:evn.dev.baseUrl,
    wxAvatarUrl: '', // 用于“用微信头像”展示
    showAvatarSheet: false,
    avatarActions: [
      { name: '用微信头像', value: 'wx' },
      { name: '从相册选择', value: 'album' },
      { name: '拍照', value: 'camera' },
    ],
  },
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      userInfo
    });
    wx.setNavigationBarTitle({ title: '账户设置' });
  },
  getChooseAvatar(e) {
    let formData = {
      openid:this.data.userInfo.openId
    }
    wx.uploadFile({
      url:  this.data.url+ '/oss/upload',
      filePath: e.detail.avatarUrl,
      name: 'file',
      formData,
      success: (res) => {
        res.data = JSON.parse(res.data)
        if(res.code==7){
          wx.showToast({
            title: '图片违规',
            icon: 'success',
            duration: 2000
          })
          return
        }
        console.log("res.avatar:"+res.data)
        this.setData({
          'userInfo.avatarUrl': res.data.fileUrl
        })
      },
    })
  },
  onCloseSheet() {
    this.setData({ showAvatarSheet: false });
  },
  onSelectWxAvatar() {
    wx.getUserProfile({
      desc: '获取微信头像',
      success: (res) => {
        this.setData({
          'userInfo.avatarUrl': res.userInfo.avatarUrl,
          wxAvatarUrl: res.userInfo.avatarUrl,
          showAvatarSheet: false,
        });
      }
    });
  },
  onSelectAlbum() {
    wx.chooseImage({
      count: 1,
      sourceType: ['album'],
      success: (res) => {
        this.setData({
          'userInfo.avatarUrl': res.tempFilePaths[0],
          showAvatarSheet: false,
        });
      }
    });
  },
  onSelectCamera() {
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: (res) => {
        this.setData({
          'userInfo.avatarUrl': res.tempFilePaths[0],
          showAvatarSheet: false,
        });
      }
    });
  },
  onInputChange(e) {
    this.setData({ 'userInfo.nickname': e.detail.value });
  },
  onGenderChange(e) {
    this.setData({ 'userInfo.gender': e.detail.value });
  },
  onPhoneChange(e) {
    this.setData({ 'userInfo.mobile': e.detail.value });
  },
 // 修改用户信息
updateWechatUserInfo() {
  let params = {
      id: this.data.userInfo.id.toString(),
      nickname: this.data.userInfo.nickname,
      avatarUrl: this.data.userInfo.avatarUrl,
      mobile: this.data.userInfo.mobile
  }
  apiUpdateUserInfo(params).then((res) => {
    if (res.code == 7) {
      wx.showModal({
        title: '内容不安全',
        content: res.msg || '昵称或头像内容不安全，请更换后再提交！',
        showCancel: false,
        confirmText: '我知道了'
      });
      return
    }
    if (res.code == 500 && res.msg && (res.msg.indexOf('内容不安全') !== -1 || res.msg.indexOf('昵称内容不安全') !== -1 || res.msg.indexOf('头像内容不安全') !== -1)) {
      wx.showModal({
        title: '内容不安全',
        content: res.msg,
        showCancel: false,
        confirmText: '我知道了'
      });
      return
    }
    wx.setStorageSync('userInfo', this.data.userInfo)
    wx.reLaunch({
      url: '/pages/members/index',
    })
  })
},
  onLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 退出逻辑
        }
      }
    });
  },
  onLogoutTap() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          wx.reLaunch({
            url: '/pages/index/login'
          });
        }
      }
    });
  },
  /**
   * 自定义性别单选按钮点击事件
   */
  onGenderChangeCustom(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({ 'userInfo.gender': value });
  },
});
